<%
layout("/layouts/platform.html"){
%>
<aside class="sidebar-250 canvas-right bg-default ">
    <header class="header navbar clearfix bb bg-default pl0 pr0">
        <p class="navbar-text"><i class="fa fa-sitemap mr5"></i> 单位列表</p>
    </header>
    <div class="content-wrap no-p">
        <div class="wrapper2">
            <div class="m5">
                <div id="jsTreeUnit"></div>
            </div>
        </div>
    </div>
</aside>
<section class="content-wrap bg-white">
    <header class="header navbar bg-white shadow">
        <div class="btn-group tool-button">
            <a id="add" class="btn btn-primary navbar-btn" href="${base}/platform/sys/role/add" data-pjax><i class="ti-plus"></i> 新建角色</a>
            <!--<button class="btn btn-danger navbar-btn" onclick="delCheck()"><i class="ti-close"></i> 删除选中</button>-->
        </div>
    </header>
    <div class=panel-body style="padding-top: 50px;">
        <div class="table-responsive no-border">
            <input id="unitid" type="hidden" value="<%if(!@shiro.hasRole('sysadmin')){%>${@shiro.getPrincipalProperty('unitid')}<%}%>">
            <table class="table table-bordered table-striped mg-t datatable">
                <thead>
                <tr>
                    <th>角色名称</th>
                    <th>权限标识</th>
                    <th>状态</th>
                    <th>操作</th>
            </table>
        </div>
    </div>

</section>
<!-- 删除角色 -->
<div id="dialogDelete" class="modal fade bs-modal-sm" tabindex="-2" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">删除角色</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        角色删除后无法恢复，并且角色下属用户将失去该角色的所有菜单权限。<br/>
                        <br/>确定删除吗？
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取  消</button>
                <button id="okDel" type="button" class="btn btn-primary" data-loading-text="正在删除...">确  定</button>
            </div>
        </div>
    </div>
</div>
<!-- 删除角色 -->
<div id="dialogDeleteCheck" class="modal fade bs-modal-sm" tabindex="-2" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">删除角色</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        角色删除后无法恢复，并且角色下属用户将失去该角色的所有菜单权限。<br/>
                        <br/>确定删除吗？
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取  消</button>
                <button id="ok" type="button" class="btn btn-primary" data-loading-text="正在删除...">确  定</button>
            </div>
        </div>
    </div>
</div>
<!-- 查看角色信息 -->
<div id="dialogDetail" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

        </div>
    </div>
</div>


<script language="JavaScript">
    var datatable;
    function initDatatable() {
        datatable = $('.datatable').DataTable({
            "dom": '<"toolbar">frtip',
            "searching":false,
            "processing": false,
            "serverSide": true,
            "select": true,
            "ordering": true,
            "language": {
                "url": "${base}/assets/plugins/datatables/cn.json"
            },
            "preDrawCallback": function () {
                sublime.showLoadingbar($(".main-content"));
            },
            "drawCallback": function () {
                sublime.closeLoadingbar($(".main-content"));
            },
            "ajax": {
                "url": "${base}/platform/sys/role/data",
                "type": "post",
                "data": function (d) {
                    d.unitid = $('#unitid').val();
                }
            },
            "order": [[0, "desc"]],
            "columns": [
                {"data": "name", "bSortable": true},
                {"data": "code", "bSortable": true},
                {"data": "disabled", "bSortable": true}
            ],
            "columnDefs": [
                {
                    "render": function (data, type, row) {
                        return '<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">' +
                                ' <i class="ti-settings"></i> <span class="ti-angle-down"></span></button><ul class="dropdown-menu" role="menu">' +
                                '<li><a href="${base}/platform/sys/role/menu/' + row.id + '" data-toggle="modal" data-target="#dialogDetail">查看权限</a></li>' +
                                '<li class="divider"></li>' +
                                '<li><a href="${base}/platform/sys/role/editMenu/' + row.id + '" data-pjax>分配权限</a></li>' +
                                '<li><a href="${base}/platform/sys/role/editUser/' + row.id + '" data-pjax>分配用户</a></li>' +
                                '<li><a href="${base}/platform/sys/role/edit/' + row.id + '" data-pjax>修改信息</a></li>' +
                                '<li class="divider"></li>' +
                                '<li><a href="javascript:;" onclick="del(\'' + row.id + '\')">删除</a></li>' +
                                '<li class="divider"></li>' +
                                '<li><a href="javascript:;" onclick="enableRole(\'' + row.id + '\')">启用</a></li>' +
                                '<li><a href="javascript:;" onclick="disableRole(\'' + row.id + '\')">禁用</a></li>' +
                                '</ul></div>';
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row) {
                        if (!data) {
                            return '<i id="disable_' + row.id + '" class="fa fa-circle text-success ml5"></i>';
                        } else {
                            return '<i id="disable_' + row.id + '" class="fa fa-circle text-danger ml5"></i>';
                        }
                    },
                    "targets": 2
                }
            ]
        });
        datatable.on('click', 'tr', function () {
            $(this).toggleClass('selected');
        });
        $("#searchBtn").on('click', function () {
            datatable.ajax.reload();
        });
    }
    var unitTreeTable;
    var selected = [];
    function initTreeView() {
        $("#jsTreeUnit").jstree({
            plugins: ["wholerow"],
            core: {
                data: {
                    url: function (node) {
                        return node.id === "#" ? "${base}/platform/sys/role/tree" : "${base}/platform/sys/role/tree?pid=" + node.id
                    }
                },
                multiple: false
            }
        }).on("select_node.jstree", function (node, selected) {
            var id = selected.selected;
            $("#unitid").val(id);
            $("#add").attr("href","${base}/platform/sys/role/add?unitid="+id);
            $("#searchForm").find("input[type='text']").val("");
            if (datatable) {
                $(".cd-panel-content").find("input").val("");
                datatable.ajax.reload();
            } else {
                initDatatable();
            }
        }).on("loaded.jstree", function (node, jstree) {
            $(node.target).find("li:first div").addClass("jstree-wholerow-clicked");
        });
    }
    function del(id) {
        var dialog = $("#dialogDelete");
        dialog.modal("show");
        dialog.find("#okDel").unbind("click");
        dialog.find("#okDel").bind("click", function (event) {
            var btn = $(this);
            btn.button("loading");
            $.post("${base}/platform/sys/role/delete/" + id, {}, function (data) {
                if (data.code == 0) {
                    datatable.ajax.reload(null,false);
                } else {
                    Toast.error(data.msg);
                }
                //重置按钮状态，关闭提示框
                btn.button("reset");
                dialog.modal("hide");
            }, "json");
        });
    }
    function delCheck() {
        var chks = datatable.rows('.selected').data();
        if (chks.length > 0) {
            var ids = [];
            $.each(datatable.rows('.selected').data(), function (i, n) {
                ids.push(n.id);
            });
            var dialog = $("#dialogDeleteCheck");
            dialog.modal("show");
            dialog.find("#ok").unbind("click");
            dialog.find("#ok").bind("click", function (event) {
                var btn = $(this);
                btn.button("loading");
                $.post("${base}/platform/sys/role/delete", {roleIds: ids.toString()}, function (data) {
                    if (data.code == 0) {
                        datatable.ajax.reload(null,false);
                    } else {
                        Toast.error(data.msg);
                    }
                    btn.button("reset");
                    dialog.modal("hide");
                }, "json");
            });
        } else {
            Toast.warning("请先选择要删除的角色！");
        }
    }
    function enableRole(id) {
        $.post("${base}/platform/sys/role/enable/" + id, {}, function (data) {
            if (data.code == 0) {
                $("#disable_" + id).attr("class", "fa fa-circle text-success ml5");
            } else {
                Toast.error(data.msg);
            }
        }, "json");
    }
    function disableRole(id) {
        $.post("${base}/platform/sys/role/disable/" + id, {}, function (data) {
            if (data.code == 0) {
                $("#disable_" + id).attr("class", "fa fa-circle text-danger ml5");
            } else {
                Toast.error(data.msg);
            }
        }, "json");
    }

    $(function () {
        initDatatable();
        initTreeView();
        $("#dialogDetail").on("hidden.bs.modal", function () {
            $(this).removeData("bs.modal");
        });
    });

</script>

<%}%>